package com.zy.cat.common.utils;

import java.net.URLEncoder;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.internal.util.WebUtils;
import com.zy.cat.entity.app.resp.NewItemResp;

import lombok.extern.log4j.Log4j2;

/**
 * @Description: 极速数据API
 * @author xiake
 * @date 2020-09-23 01:42:11
 */
@Log4j2
public class JISUAPI {

	public static final String APPKEY = "b553860ea755890d";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/news/get";
	public static final String channel = "头条";// utf8 新闻频道(头条,财经,体育,娱乐,军事,教育,科技,NBA,股票,星座,女性,健康,育儿)
	public static final int num = 30;// 数量 默认10，最大40

	private static JISUAPI instance;

	public static JISUAPI getInstance() {
		if (instance == null) {
			instance = new JISUAPI();
		}
		return instance;
	}

	/**
	 * 
	 * @author lipengchao
	 * @date 2020-09-23 02:58:26
	 * @param channelName
	 * @param pageSize
	 * @param pageNum
	 */
	public List<NewItemResp> getNew(String channelName, int pageSize, int pageNum) {
		String result = null;
		try {
			String url = URL + "?channel=" + URLEncoder.encode(channelName, "utf-8") + "&num=" + pageSize + "&start="
					+ pageNum + "&appkey=" + APPKEY;
			result = WebUtils.doGet(url, null);
			JSONObject json = JSON.parseObject(result);
			if (json.getInteger("status") != 0) {
//              System.out.println(json.getString("msg"));
				log.error("获取新闻数据失败:"+json.getString("msg"));
			} else {
				JSONObject resultarr = (JSONObject) json.getJSONObject("result");
				String channel = resultarr.getString("channel");
				String num = resultarr.getString("num");
				List<NewItemResp> resultList = JSON.parseArray(resultarr.getString("list"), NewItemResp.class);
				return resultList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
